11. Quiz: Test Returns for Statistical Significance
Test Returns for Statistical Significance
Complete the function
analyze_returns
below.
Remember that if the sample mean is \bar{x} , and the sample standard deviation, s , equals:
s = \sqrt{\frac{1}{n-1}\sum_{i=1}^n{(x_i-\bar{x})}^2}
then the standard error, s_{\bar{x}} , equals:
s_{\bar{x}} = \frac{s}{\sqrt{n}}
where n is the number of observations in the sample. Then in the case that we are testing the hypothesis that the population mean, \mu , equals 0, the t-statistic, t , equals:
t = \frac{\bar{x}-\mu}{s_{\bar{x}}} = \frac{\bar{x}-0}{s_{\bar{x}}} = \frac{\bar{x}}{s_{\bar{x}}}
This link
provides a useful reference on the
scipy.stats
package.
This part
is about the t-test.
Start Quiz:
import pandas as pd
import numpy as np
import scipy.stats as stats
def analyze_returns(net_returns):
"""
Perform a t-test, with the null hypothesis being that the mean return is zero.
Parameters
----------
net_returns : Pandas Series
A Pandas Series for each date
Returns
-------
t_value
t-statistic from t-test
p_value
Corresponding p-value
"""
# TODO: Perform one-tailed t-test on net_returns
# Hint: You can use stats.ttest_1samp() to perform the test.
# However, this performs a two-tailed t-test.
# You'll need to divde the p-value by 2 to get the results of a one-tailed p-value.
null_hypothesis = 0.0
return None
def test_run(filename='net_returns.csv'):
"""Test run analyze_returns() with net strategy returns from a file."""
net_returns = pd.Series.from_csv(filename, header=0)
t, p = analyze_returns(net_returns)
print("t-statistic: {:.3f}\np-value: {:.6f}".format(t, p))
if __name__ == '__main__':
test_run()
date,return
2014-11-30,0.011635519414351009
2014-12-31,0.021954092308170955
2015-01-31,0.04088726605224263
2015-02-28,-0.0025112428290965105
2015-03-31,0.01725861669076579
2015-04-30,0.04169672034614155
2015-05-31,0.033973104078552634
2015-06-30,0.011808567427224085
2015-07-31,-0.034296712711723694
2015-08-31,-0.018000336527001545
2015-09-30,0.04244859300243494
2015-10-31,0.06048679384582347
2015-11-30,0.004425442647837301
2015-12-31,-0.011128068116992282
2016-01-31,0.03904059055851628
2016-02-29,-0.018534757459136356
2016-03-31,0.051492028368816355
2016-04-30,0.008987284920489497
2016-05-31,0.04030096220308846
2016-06-30,-0.07014813509500822
2016-07-31,-0.03744600404974712
2016-08-31,-0.05765103830475751
2016-09-30,0.05280791013124859
2016-10-31,0.04813615235947365
2016-11-30,0.019069757993200993
2016-12-31,0.02369364599868217
2017-01-31,-0.022749483402672258
2017-02-28,-0.07743936042570741
2017-03-31,0.03324798542019691
2017-04-30,-0.02091584879987623
2017-05-31,-0.06955420357572517
2017-06-30,0.003758874907665249